package com.ty.config;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

/** @author Mjg @QQ:2091864671 @Creates by mjg ON 2020/6/25-15:46 */
@Component
public class MyFilter implements Filter {

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {}

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    HttpServletResponse resp = (HttpServletResponse) response;
    HttpServletRequest req = (HttpServletRequest) request;
    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
    resp.setHeader("Access-Control-Max-Age", "3600");
    resp.setHeader(
        "Access-Control-Allow-Headers",
        "x-requested-with, Content-Type, Accept, Origin,Authorization,tag");
    resp.setHeader("Access-Control-Allow-Credentials", "true");
    chain.doFilter(req, resp);
  }

  @Override
  public void destroy() {
    System.err.println("过滤器结束-----------------------");
  }
}
